/*

  The MIT License

  Copyright (c) 2008 - 2011. Dimarik

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

*/

package ru.goodcore.display.ui
{

import flash.display.Graphics;
import flash.errors.IllegalOperationError;
import flash.geom.Rectangle;

import ru.goodcore.core.CanvasTracer;
import ru.goodcore.core.ILayoutTracer;
import ru.goodcore.datastructure.Enum;
import ru.goodcore.display.ui.core.SizeDependet;
import ru.goodcore.display.ui.core.UIComponent;

/**
 * 
 */
public class Container extends UIComponent {

	//-------------------------------------------------------------------------
	//
	//   Constructor
	//
	//-------------------------------------------------------------------------

	/**
	 * Constructor
	 */
	public function Container() {
		super();
		this.clipContent = true;
	}

	//-------------------------------------------------------------------------
	//
	//   Overridden properties:	DisplayObject
	//
	//-------------------------------------------------------------------------
/*
	public override function get graphics():Graphics {
		Error.throwError(IllegalOperationError, 1001, '(getter) UIComponent#graphics');
		return null;
	}
*/
	//-------------------------------------------------------------------------
	//
	//   Overridden properties:	UIComponent
	//
	//-------------------------------------------------------------------------

	//---------------------------------
	//  width
	//---------------------------------
	/**
	 * @param value
	 */	
	public override function set width(value:Number):void {
		super.width = value;
	}

	//-------------------------------------------------------------------------
	//
	//   Properties
	//
	//-------------------------------------------------------------------------

	//---------------------------------
	//  color
	//---------------------------------
	/**
	 * @private
	 */
	private var _color:		uint = 0xFF0000;

	public function set color(value:uint):void {
		if (this._color == value) return;
		this._color = value;
		super.invalidate();
	}

	public function get color():uint {
		return this._color;
	}

	//---------------------------------
	//  clipContent
	//---------------------------------
	/**
	 * @private
	 */
	private var _clipContent:			Boolean = false;
	private var _clipContentChanged:	Boolean = false;

	public function set clipContent(value:Boolean):void {
		if (this._clipContent == value) return;
		this._clipContent = value;
		this._clipContentChanged = true;
		super.invalidate();
	}

	public function get clipContent():Boolean {
		return this._clipContent;
	}

	//---------------------------------
	//   layoutTracer
	//---------------------------------
	/**
	 * @private 
	 */	
	private var _layoutTracer:	ILayoutTracer;
	
	protected function set layoutTracer(value:ILayoutTracer):void {
		if (this._layoutTracer === value)
			return;
		
		this._layoutTracer = value;
		this.invalidate();
	}
	
	protected function get layoutTracer():ILayoutTracer {
		if (!this._layoutTracer)
			this._layoutTracer = new CanvasTracer();
		
		return this._layoutTracer;
	}


	//-------------------------------------------------------------------------
	//
	//   Private variables
	//
	//-------------------------------------------------------------------------

	//-------------------------------------------------------------------------
	//
	//   Public methods
	//
	//-------------------------------------------------------------------------


	/**
	 * @private
	 */
	public override function validate():void {
		trace('Container#validate');

		if ( super.sizeChanged ) {
			const g:Graphics = super.graphics;
			g.clear();

			if (super.width && super.height) {
				g.beginFill(this._color, 1);
				g.drawRoundRect(0, 0, super.width, super.height, 15, 15);
				g.endFill();
			}

			// Validate self first
			if (super.numChildren)
				this.layoutTracer.layoutObjects(this);

			var parent:Container = super.parent as Container;

			// Invalidate parent
			if (parent is Container) // let the parent relayout us 
				(parent as Container).invalidateChildList();

		}

		if ( this._clipContentChanged ) {
			if (this._clipContent) {
				var rect:Rectangle = super.scrollRect;
				if (rect) {
					rect.width = this.width;
					rect.height = this.height;
					super.scrollRect = rect;
				} else {
					rect = super.getRect(this);
					rect.x = 0;
					rect.y = 0;
					super.scrollRect = rect;
				}
			} else {
				super.scrollRect = null;
			}
		}

		super.validate();
	}

	/**
	 * 
	 * 
	 */	
	public function invalidateChildList():void {
		this.layoutTracer.layoutObjects(this);
	}

	//-------------------------------------------------------------------------
	//
	//   Event handlers
	//
	//-------------------------------------------------------------------------

}
}